package com.awesome.todo.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is a basic subclass of the SQLiteOpenHelper. Its
 * job is simply to create/upgrade the user table.
 * @author doug
 *
 */
public class UserTable extends SQLiteOpenHelper {

	private static final String DATABASE_NAME 	= "usertable.db";
	private static final int DATABASE_VERSION 	= 1;
	public static final String TABLE_USER 		= "user";
	public static final String COLUMN_ID 		= "_id";
	public static final String COLUMN_NAME 		= "name";
	public static final String COLUMN_EMAIL 	= "email";
	public static final String COLUMN_LOGIN 	= "login";
	public static final String COLUMN_PASSWORD 	= "password";
	public static final String DATABASE_CREATE 	= "create table " + TABLE_USER
													+ "(" 
													+ COLUMN_ID + " integer primary key autoincrement, "
													+ COLUMN_NAME + " text not null, " 
													+ COLUMN_EMAIL + " text not null, " 
													+ COLUMN_LOGIN + " text not null, "
													+ COLUMN_PASSWORD + " text not null" 
													+ ");";

	public UserTable(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * Creates the table if it doesn't exist.
	 */
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}
	
	/**
	 * Upgrades the table from old version to new version.
	 */
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		Log.w(UserTable.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will desetroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
		onCreate(database);
	}

}